//
//  XQToolManager.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/29.
//

#import "XQToolManager.h"
#import "XQDevice.h"
#import "XQNetworkType.h"
#include <sys/sysctl.h>
#include <mach/mach.h>
#import "XQHelper.h"
#import <SystemConfiguration/CaptiveNetwork.h>
#import <NetworkExtension/NetworkExtension.h>

@implementation XQToolManager

+ (instancetype)manager {
    static XQToolManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (BOOL)needLocationPop {
    return self.model.accomplish.telex == 1;
}

- (NSString *)loginProtocol {
    return self.model.accomplish.join_private_url;
}

- (NSDictionary *)getLoacationInfo {
    
    if (!self.placemark || !self.location)return @{};
    
    // 获取位置信息
    NSString *country = self.placemark.country; // 国家
    NSString *administrativeArea = self.placemark.administrativeArea; // 省份
    NSString *locality = self.placemark.locality; // 市
    NSString *subLocality = self.placemark.subLocality; // 区
    NSString *street = self.placemark.thoroughfare; // 街道
    NSString *countryCode = self.placemark.ISOcountryCode; // 国家代码
    CLLocationDegrees latitude = self.location.coordinate.latitude; // 纬度
    CLLocationDegrees longitude = self.location.coordinate.longitude; // 经度
   
    return @{
        @"methods": latitude == 0 ? @"0" : [NSString stringWithFormat:@"%.6f",longitude],
        @"mitigating":longitude == 0 ? @"0" : [NSString stringWithFormat:@"%.6f",latitude],
        @"introduce": XQUtils.safeString(country),
        @"sunshade":XQUtils.safeString(countryCode),
        @"marginally":XQUtils.safeString(administrativeArea),
        @"wrote":XQUtils.safeString(locality),
        @"essentially":XQUtils.safeString(subLocality),
        @"coordinate":XQUtils.safeString(street)
    };
}

- (NSString *)getDeviceInfo {
    
    NSDictionary *totalDic = @{
        @"masking" : [self memoryInfo],
        @"representatives" : self.representatives == nil ? @{} : self.representatives,
        @"transit" : [self getTransit],
        @"white" : [self getWhite],
        @"recorded" : [self getRecord],
        @"exploration" : [self getExploration]
    };
    
    return [totalDic yy_modelToJSONString];
}

// 内存
- (NSDictionary *)memoryInfo {
    return @{
        @"pixel" : [XQHelper getNewFreeSize],
        @"mitigated" : [XQHelper getTotalDiskSize],
        @"easily" : [XQHelper getTotalMemorySize],
        @"minimal" : [XQHelper getAvailableMemorySize],
    };
}

- (NSDictionary *)getTransit {
    return @{
        @"correlated" : [XQDevice getSystemVersion],
        @"shot" : [XQDevice getSystemName],
        @"decam" : [XQDevice getOriginalDeviceModel]
    };
}

- (NSDictionary *)getWhite {
    return @{
        @"clarification" : [self isSimuLator] ? @"1" : @"0",
        @"strong" : [self isJailbroken] ? @"1" : @"0"
    };
}

- (NSDictionary *)getRecord {
    
    // 获取当前时区
    NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
    
    // 获取与 GMT 的偏移量（以秒为单位）
    NSInteger secondsFromGMT = [currentTimeZone secondsFromGMT];
    
    // 计算小时
    NSInteger hours = secondsFromGMT / 3600;
    
    NSString *gmtOffset = [NSString stringWithFormat:@"GMT%+ld", (long)hours];
    
    return @{
        @"ctio" : gmtOffset,
        @"making" : [XQDevice getIDFV],
        @"flare" : [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode],
        @"visible" : [XQNetworkType getNetworkType],
        @"bright" : [XQDevice getIDFA]
    };
}

- (NSDictionary *)getExploration {
    
    NSArray *interfaces = (__bridge NSArray *)CNCopySupportedInterfaces();

    NSString *SSID = @"";
    NSString *BSSID = @"";
    for (NSString *interface in interfaces) {
        NSDictionary *info = (__bridge NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)interface);
        if (info) {
            if ([info objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID]) {
                SSID = [info objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
                BSSID = [info objectForKey:(__bridge NSString *)kCNNetworkInfoKeyBSSID];
            }
        }
    }
    
    return @{
        @"obstacles" : @{
            @"man" : BSSID,
            @"chance" : SSID
        }
    };
}

+ (UIViewController *)appRootViewController {
    UIApplication *application = [UIApplication performSelector:@selector(sharedApplication)];
    UIViewController *appRootVC = application.delegate.window.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}

- (BOOL)isJailbroken {
    if ([[NSFileManager defaultManager] fileExistsAtPath:@"/User/Applications/"]) {
//        NSArray *applist = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/User/Applications/" error:nil];
        return YES;
    }
    return NO;
}


- (NSString *)countryLogo {
    NSString *imageName = self.model.accomplish.disaster.fax;
    return imageName;
}

- (NSString *)phoneScenarios {
    return self.model.accomplish.disaster.scenarios;
}

-(BOOL)isSimuLator
{
    if (TARGET_IPHONE_SIMULATOR == 1 && TARGET_OS_IPHONE == 1) {
        //模拟器
        return YES;
    }else{
        //真机
        return NO;
    }
}


//+ (NSDictionary *)getCurrentWiFiInfo {
//NSMutableDictionary *wifiInfo = [NSMutableDictionary dictionary];
//// 获取支持的接口，这里我们只关心WiFi
//NSArray *interfaces = (__bridge NSArray *)CNCopySupportedInterfaces();
//for (NSString *interface in interfaces) {
//NSDictionary *info = (__bridge NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)interface);
//if (info) {
//// 我们只关心WiFi接口
//if ([info objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID]) {
//wifiInfo[@"SSID"] = [info objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
//wifiInfo[@"BSSID"] = [info objectForKey:(__bridge NSString *)kCNNetworkInfoKeyBSSID];
//}
//}
//}
//return wifiInfo;
//}



@end
